package Or

import (
	"database/sql"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/ref"
	"strings"
)

func (vm *ViewMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(vm, parameter, value)
}

func (vm ViewMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (vm *MViewMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(vm, parameter, value)
}

func (vm MViewMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (vm ViewMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	vm.Parameter.Con = Con
	for _, v := range []string{"ALL_VIEWS", "DBA_VIEWS"} {
		execSql = append(execSql, fmt.Sprintf("select "+
			"VIEW_NAME from "+
			"%v "+
			"where 1=1 and "+
			"OWNER = '%v' %v", v, vm.Parameter.Object.Schema, func() string {
			switch {
			case vm.Parameter.Object.Table == "*":
				return ""
			case strings.Contains(vm.Parameter.Object.Table, "%"):
				return fmt.Sprintf("and VIEW_NAME like '%v'", vm.Parameter.Object.Table)
			default:
				return fmt.Sprintf("and VIEW_NAME = '%v'", vm.Parameter.Object.Table)
			}
		}()))
	}
	if result, err = forExecQuerySql(event, vm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (vm MViewMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	vm.Parameter.Con = Con
	for _, v := range []string{"ALL_MVIEWS", "DBA_MVIEWS"} {
		execSql = append(execSql, fmt.Sprintf("select "+
			"MVIEW_NAME from "+
			"%v "+
			"where 1=1 and "+
			"OWNER = '%v' %v", v, vm.Parameter.Object.Schema, func() string {
			switch {
			case vm.Parameter.Object.Table == "*":
				return ""
			case strings.Contains(vm.Parameter.Object.Table, "%"):
				return fmt.Sprintf("and MVIEW_NAME like '%v'", vm.Parameter.Object.Table)
			default:
				return fmt.Sprintf("and MVIEW_NAME = '%v'", vm.Parameter.Object.Table)
			}
		}()))
	}
	if result, err = forExecQuerySql(event, vm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (vm ViewMe) Existence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	vm.Parameter.Con = Con
	var execSql []string
	for _, v := range []string{"ALL_VIEWS", "DBA_VIEWS"} {
		execSql = append(execSql, fmt.Sprintf("select count(*) as \"sum\" from %v where owner = '%s'  and VIEW_NAME = '%s'", v, vm.Parameter.Object.Schema, vm.Parameter.Object.Table))
	}
	if result, err = forExecQuerySql(event, vm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	var status int64
	if status, err = stringAnyToInt64(result.Result); err != nil {
		return
	}
	switch status {
	case 1:
		result.Result = true
	case 0:
		result.Result = false
	}
	return
}
func (vm MViewMe) Existence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	vm.Parameter.Con = Con
	var execSql []string
	for _, v := range []string{"ALL_MVIEWS", "DBA_MVIEWS"} {
		execSql = append(execSql, fmt.Sprintf("select count(*) as \"sum\" from %v where owner = '%s'  and MVIEW_NAME = '%s'", v, vm.Parameter.Object.Schema, vm.Parameter.Object.Table))
	}
	if result, err = forExecQuerySql(event, vm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	var status int64
	if status, err = stringAnyToInt64(result.Result); err != nil {
		return
	}
	switch status {
	case 1:
		result.Result = true
	case 0:
		result.Result = false
	}
	return
}
func (vm ViewMe) ViewDrop(Con *sql.DB) (result global.Return, err error) {
	//var event = "[ViewDrop]"
	//vm.Parameter.Con = Con
	//result.Sql = fmt.Sprintf("DROP VIEW IF EXISTS %v.%v", vm.Parameter.Object.Schema, vm.Parameter.Object.Table)
	//if result.TimeOut, result.Result, err = newExecUpdateSql(ExecSqlInputS{
	//	Ss:           vm.Parameter,
	//	Event:        event,
	//	UpdateSqlStr: []string{result.Sql},
	//}); err != nil {
	//	return
	//}
	return
}
func (vm MViewMe) ViewDrop(Con *sql.DB) (result global.Return, err error) {
	//var event = "[ViewDrop]"
	//vm.Parameter.Con = Con
	//result.Sql = fmt.Sprintf("DROP MATERIALIZED  IF EXISTS %v.%v", vm.Parameter.Object.Schema, vm.Parameter.Object.Table)
	//if result.TimeOut, result.Result, err = newExecUpdateSql(ExecSqlInputS{
	//	Ss:           vm.Parameter,
	//	Event:        event,
	//	UpdateSqlStr: []string{result.Sql},
	//}); err != nil {
	//	return
	//}
	return
}
func (vm ViewMe) ViewDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
func (vm MViewMe) ViewDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
